/*
Produces the three panels of Table 1
*/




/*
Panel A - Unbalanced panel
*/


set more off
clear
use user_quarter_choice_cholesterol


// focus on cholesterol
tab indic_id
keep if indic_id == 5


// BASIC: LOOK AT IMPACT OF FIRST CHOICE ON LATER CHOICE 
egen first_q = min(quarter), by(enrolid)
gen first_choice_temp = generic_id if quarter == first_q
egen first_choice = max(first_choice_temp), by(enrolid)

gen first_generic_temp = generic if quarter == first_q
egen first_generic = max(first_generic_temp), by(enrolid)

gen first_xr_temp = xr if quarter == first_q
egen first_xr = max(first_xr_temp), by(enrolid)


gen period = quarter - first_q
gen chose_initial = (generic_id == first_choice) & (generic == first_generic) & (xr == first_xr)


eststo clear



gen initial_generic_temp = generic if quarter == first_q
egen initial_generic_user = max(initial_generic_temp), by(enrolid)

drop first_choice_temp first_choice


// need name for overlap analysis
merge m:1 generic_id using generic_name_mapping
keep if _merge == 3
drop _merge


rename generic_id curr_generic_id
rename gennme curr_gennme
rename generic curr_generic
rename xr curr_xr
drop drugid

// user is in the dataset only based on first use
joinby enrolid indic_id using user_treatment_indicators_cholesterol


// filter out irrelevant entries
// 1) not either treatment or control for that entry event
keep if treat == 1 | control == 1
replace treat = 0 if control == 1

//replace treat_alt = 0 if control_alt == 1

// 2) quarter came before entry quarter
gen entry_quarter = qofd(entry_date)
gen rel_quart = quarter - entry_quarter
//keep if rel_quart >= 0

// A) generate the outcomes
gen chose_entry_drug = (curr_generic_id == generic_id) & (curr_generic == generic) & (curr_xr == xr)
tab chose_entry_drug


// B) analyze
gen treat_generic = treat * generic 
gen treat_xr = treat * xr
// is stickiness bigger for generic choice? (doesn't have to be)


// Does this need correcting?
gen initial = chose_entry_drug if quarter == first_q  // rel_quart == 0 (old code; this is probably cleaner; use their first choice, otherwise some not there in period 0)
egen initial_choice = max(initial), by(enrolid entry_num) // unnecessary? maybe only 1 entry_num per enrolid


//////////////////////////
// IV - Breakdown (B/G) //
//////////////////////////


forvalues i=3(3)15 {

	
	display "Offset: `i' quarters"	
	eststo: ivregress 2sls chose_entry_drug (initial_choice=treat) if rel_quart == `i' & generic == 0, r	
}

esttab using table_1A.tex, con r2 se label replace booktabs keep(initial_choice) title(Regression table \label{tab1})
eststo clear





/*
Panel B - Inpatient starters
1) Create a set of users starting in a quarter with an inpatient event
2) Re-run the same analysis
*/
preserve
merge m:1 enrolid using user_starting_hospital
keep if _merge == 3
drop _merge


forvalues i=3(3)15 {

	
	display "Offset: `i' quarters"	
	eststo: ivregress 2sls chose_entry_drug (initial_choice=treat) if rel_quart == `i' & generic == 0, r	
}

esttab using table_1B.tex, con r2 se label replace booktabs keep(initial_choice) title(Regression table \label{tab1})
eststo clear

restore



/*
Panel C - Balanced Panel
*/

merge m:1 enrolid using "panel_enrolid_2003_2012"
keep if _merge == 3
drop _merge


forvalues i=3(3)15 {

	
	display "Offset: `i' quarters"	
	eststo: ivregress 2sls chose_entry_drug (initial_choice=treat) if rel_quart == `i' & generic == 0, r	
}

esttab using table_1C.tex, con r2 se label replace booktabs keep(initial_choice) title(Regression table \label{tab1})
eststo clear







